//  数据操作JS
var opt = {
    //  保存数据
    saveDatas: function (obj, dataType, fName) {
        fName = fName || "myForm";
        console.log(dataType, fName)
        switch (dataType) {
            case "menus": //  菜单
                var menuType = $("input[name='menutypes']:checked").val();
                $("#menutype").val(menuType);
                $("#isEffective").val($("input[name='isEffectives']").is(':checked') ? 1 : 0);
                break;
            case "jobs":
                $("#deptcode").val($("#Hiddeptcode").val());
                break;
            case "users":
                $("#deptcode").val($("#Hiddeptcode").val());
                var isSale = $("input[name='sales']:checked").val();
                $("#isSale").val(isSale);
                if ($("#jobcode").val().length == 0) {
                    com.alterMessage("请选择职务名称！", "warning");
                    return;
                }
                //  获取自定义数据
                com.setDefinedVal('d-', 'jsondatas', "#" + fName);
                break;
            case "formCustoms":
                var options = [];
                $("input[name^='optionname-']").each(function () {
                    var strOptionItems = {};
                    strOptionItems["value"] = $(this).val();
                    options.push(strOptionItems);
                });
                $("#optionjson").val(JSON.stringify(options));
                $("#modulename").val("users");
                break;
            case "dics":
                var disc = [];
                $("input[name^='dicname-']").each(function () {
                    disc.push($(this).val());
                });
                $("#dicsJson").val(disc.toString());
                break;
            case "tasklst":
                $("#sendtaskuser, #taskfirstdate").attr("disabled", false);
                break;
            case "inquiry":
                $("#sales, #inquirydate").attr("disabled", false)

                break;
        }
        if ($('#' + fName + '').valid()) {
            console.log($("#myForm").serialize())
            $.ajax({
                url: com.loadUrl(dataType, 0),
                type: 'post',
                dataType: 'json',
                data: $("#" + fName).serialize(),
                async: false,
                success: function (result) {
                    if (result.resultCode == 200) {
                        setTimeout(function () {
                            switch (dataType) {
                                case "menus":
                                    if (!$("#menuid").val() || $("#menuid").val().length == 0) {
                                        com.queryDatas(dataType);
                                    } else {
                                        GM.updateRowData('table-tree', 'menuid', result.data);
                                    }
                                    break;
                                case "formCustoms":
                                    document.querySelector('#table-defined').GM('setQuery', {});
                                    break;
                                default:
                                    com.queryDatas(dataType);
                                    break;
                            }
                            $("button[name='bt-close']").click();
                        }, 2000);
                        com.alterMessage("保存成功！", "success");
                    } else {
                        com.alterMessage(result.msg, "warning");
                    }
                    $(obj).removeAttr("disabled");
                }
            });
        } else {
            return false;
        }
    },
    //  用户信息
    saveUsersInfo: function (obj) {
        if ($('#myForm').valid()) {
            $.ajax({
                url: '../users/saveUsersInfo',
                type: 'post',
                dataType: 'json',
                data: $("#myForm").serialize(),
                async: false,
                success: function (result) {
                    $.cookie("userName", result.data.name);
                    $.cookie("userImg", result.data.imgurl);
                    $("#Img-User").attr("src", result.data.imgurl);
                    $("#Span-UserName").attr("src", result.data.name);
                    if (result.resultCode == 200) {
                        setTimeout(function () {
                            $("button[name='bt-close']").click();
                        }, 2000);
                        com.alterMessage("保存成功！", "success");
                    } else {
                        com.alterMessage(result.msg, "warning");
                    }
                    $(obj).removeAttr("disabled");
                }
            });
        } else {
            return false;
        }
    },
    //  保存权限数据
    saveAuths: function (obj, dataType) {
        var domTree = dataType == "userDepts" ? "treeDepts" : "treeMenus";
        var treeObj = $.fn.zTree.getZTreeObj(domTree),
            nodes = treeObj.getCheckedNodes(true),
            v = [];
        for (var i = 0; i < nodes.length; i++) {
            v.push(nodes[i].id);
        }
        if (v.length == 0) {
            com.alterMessage("请选择权限菜单", "warning");
            return;
        }
        var data = {moduleItems: v.join(',')};
        var url = "";
        switch (dataType) {
            case "jobMenus":
                data["jobCode"] = $("#Hidjobcode").val();
                url = '../comp/saveJobMenus';
                break;
            case "userDepts":
                data["userId"] = $("#userid").val();
                url = '../users/saveUserDepts';
                break;
            case "userMenus":
                data["userId"] = $("#userid").val();
                url = '../users/saveUserMenus';
                break;
        }
        $.ajax({
            url: url,
            type: 'post',
            dataType: 'json',
            data: data,
            async: false,
            success: function (result) {
                if (result.resultCode == 200) {
                    setTimeout(function () {
                        $("button[name='bt-close']").click();
                    }, 2000);
                    com.alterMessage("保存成功！", "success");
                    $("#bt-complate").attr("disabled",true);
                } else {
                    com.alterMessage(result.msg, "warning");
                }
                $(obj).removeAttr("disabled");
            }
        });
    },
    //  阅读公告
    readNotices: function (dataType) {
        $.ajax({
            url: '../operate/readNotices',
            type: 'post',
            dataType: 'json',
            data: {dataId: $("#Hidnid").val(), dataType: dataType},
            async: false,
            success: function (result) {
                if(dataType == 1){
                    $("#small-chat").hide();
                }
                $(".open-small-chat").click();
            }
        });
    },
    //  设置数据排序
    setDataSn: function (e, snType, dataType) {
        $.ajax({
            url: '../operate/setDataSn',
            type: 'post',
            dataType: 'json',
            data: {dataId: e, snType: snType, dataType: dataType},
            async: false,
            success: function (result) {
                if (result.resultCode == 200) {
                    setTimeout(function () {
                        com.queryDatas(dataType);
                        $("button[name='bt-close']").click();
                    }, 2000);
                    com.alterMessage("保存成功！", "success");
                } else {
                    com.alterMessage(result.msg, "warning");
                }
                $(obj).removeAttr("disabled");
            }
        });
    },
    //  修改消息状态
    updateMsgStatus: function (dataId) {
        $.ajax({
            url: '../sys/updateMsgStatus',
            type: 'post',
            dataType: 'json',
            data: {dataId: dataId},
            async: false,
            success: function (result) {
                com.queryDatas('msgs');
            }
        });
    },
    //  删除数据
    deleteDatas: function (dataId, dataType) {
        $.ajax({
            url: '../operate/deleteDatas',
            type: 'post',
            dataType: 'json',
            data: {"dataId": dataId, "dataType": dataType},
            async: false,
            success: function (result) {
                if (result.resultCode == 200) {
                    setTimeout(function () {
                        com.queryDatas(dataType);
                    }, 2000);
                    com.alterMessage("删除成功！", "success");
                } else {
                    com.alterMessage(result.msg, "warning");
                }
            }
        });
    },
    //  批量删除数据
    deleteAllDatas: function (dataIds, dataType, tName) {
        tName = !tName ? "table-data" : tName;
        console.log(dataType)
        $.ajax({
            url: '../operate/deleteAllDatas',
            type: 'post',
            dataType: 'json',
            data: {"dataIds": dataIds, "dataType": dataType},
            async: false,
            success: function (result) {

                $("#bt-del").attr("disabled", true);
                if (result.resultCode == 200) {
                    setTimeout(function () {
                        com.queryDatas(dataType);
                    }, 2000);
                    com.alterMessage("删除成功！", "success");
                } else {
                    com.alterMessage(result.msg, "warning");
                    console.log(result.msg)
                }
            }
        });
    },
    //  提交数据
    submitDatas : function (dataId, dataType) {
        $.ajax({
            url: '../operate/submitDatas',
            type: 'post',
            dataType: 'json',
            data: {"dataId": dataId, "dataType": dataType},
            async: false,
            success: function (result) {
                if (result.resultCode == 200) {
                    setTimeout(function () {
                        com.queryDatas(dataType);
                    }, 2000);
                    com.alterMessage("操作成功！", "success");
                    $(".fa-trash","#bt-del").attr("disabled", true);
                    $("#bt-del").remove().addClass(".fa-search");
                    let usersCookie = $.cookie("users");
                    let users = JSON.parse(usersCookie);
                    console.log(users)
                } else {
                    com.alterMessage(result.msg, "warning");
                }
            }
        });
    },
    //  导出Excel数据
    exportDatas : function (dataId,dataType){

    },
    // 撤销
    takebackDatas: function (dataId, dataType){
        $.ajax({
            url: "../operate/takebackDatas",
            type: 'post',
            dataType: 'json',
            data: {"dataId" : dataId, "dataType" : dataType},
            async: false,
            success: function (result){
                if(result.resultCode == 200){
                    setTimeout(function () {
                        com.queryDatas(dataType);
                    }, 2000);
                    com.alterMessage("操作成功！", "success");
                    $("#bt-complate").attr("disabled",true);

                } else {
                    com.alterMessage(result.msg, "warning");
                }
            }
        })
    },
    // 完成
    FromComplate : function (dataId, dataType) {
        console.log(dataId, dataType)
        $.ajax({
            url: '../operate/ComplateDatas',
            type: 'post',
            dataType: 'json',
            data: {"dataId": dataId, "dataType": dataType},
            async: false,
            success: (result) => {
                if(result.resultCode == 200) {
                    setTimeout(() => {
                        com.queryDatas(dataType);
                    },2000);
                    com.alterMessage(result.msg, "success");
                    $(".fa-trash",".fa-send",".fa-reply").attr("disabled",true);
                    $(".fa-edit").remove().addClass(".fa-search");
                }else {
                    com.alterMessage(result.msg,"warining");
                }
            }
        });
    }

}